﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Windows;
using HeadLinx.AD.Common;
using System.IO;
using System.Diagnostics;

namespace HeadLinx.AD.Explorer
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private HeadLinxPhotoCache _photoCache;

        //protected override void OnStartup(StartupEventArgs e)
        //{
        //    if (e.Args.Length > 0 && e.Args[0] == "prototype")
        //    {
        //        this.StartupUri = new Uri("MainMenuWindow.xaml", UriKind.Relative);
        //    }
        //    else
        //    {
        //        this.StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
        //    }

        //    base.OnStartup(e);
        //}

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            const string THUMBNAIL_PHOTO_CACHE_FOLDER = "HeadLinx Thumbnail Photos";

            try
            {
                App.PhotoCache = new HeadLinxPhotoCache(THUMBNAIL_PHOTO_CACHE_FOLDER);
            }
            catch (Exception ex)
            {
                App.PhotoCache = null;
                Trace.TraceWarning("Thumbnail photos will not be cached due error creating cache folder: " + ex.Message);
                //MessageBox.Show("Thumbnail photos will not be cached due error creating cache folder:"
                //    + Environment.NewLine + ex.Message,
                //    "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            }

            if (e.Args.Length > 0)
            {
                string inputArg = e.Args[0];

                this.ShutdownMode = ShutdownMode.OnExplicitShutdown;

                try
                {
                    if (inputArg.Contains(@"\"))
                    {
                        // Assume command line argument is a file path.
                        if (!File.Exists(inputArg))
                        {
                            throw new FileNotFoundException();
                        }

                        // Show chart and block.
                        LocalUtility.LoadAndShowChartFile(inputArg, true);
                    }
                    else
                    {
                        // Assume command line argument is a list of names.
                        LocalUtility.RunQueryAndShowResults(null, HeadLinxQueryType.Arbitrary, inputArg, null, true);
                    }

                    Application.Current.Shutdown(0);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error processing " + inputArg + ":" + Environment.NewLine + ex.Message, "File Load Error");
                    Application.Current.Shutdown(1);
                }
            }
            else
            {
                MainMenuWindow w = new MainMenuWindow();
                w.Show();
            }
        }

        public static HeadLinxPhotoCache PhotoCache
        {
            get
            {
                return ((App)Application.Current)._photoCache;
            }
            set
            {
                ((App)Application.Current)._photoCache = value;
            }
        }

    }
}
